<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/12
 * Time: 17:34
 */

namespace Home\Controller;

use \Think\Controller;

use \Tools\AuthController;

class MessageController extends AuthController
{
    //咨询管理
    function consult()
    {
        $this->NewProduct();
    }

    // 获取头菜单
    function show_top()
    {
        $level = D('ssg_modules');
        $this->assign("pid", $level->where("name='咨询管理'")->find());
        $this->assign("list", $level->where("level=2")->select());
    }

    //新品推荐
    function NewProduct()
    {
        $this->show_top();
        $page = new \Tools\paged();
        $arr = $page->paged('ssg_new_recommend', 6, $_GET['find_goods'],array('title','goodsCode','time'));
        $this->assign('page_list', $arr['page']->fpage());
        $this->assign("goods_list", $arr['result']);
        $this->display('newproduct');
    }

    //处理删除信息
    function deleteNewGoods()
    {
        if (!empty($_POST)) {
            $ssg_new = D($_POST['which_DB']);
            $delete = $ssg_new->where("id='" . $_POST['id'] . "'")->delete();
            if ($delete) {
                echo "{\"result\":\"删除成功\"}";
            } else {
                echo "{\"result\":\"不存在\"}";
            }
        } else {
            echo "{\"error\":\"删除失败\"}";
        }
    }

    //查看预编辑信息
    function updateNewGoods()
    {
        $ssg_new = D($_POST['which_DB']);
        $result = $ssg_new->where("id='" . $_POST['id'] . "'")->find();

        if ($result) {
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        } else {
            echo "{\"error\":\"1\"}";
        }

    }

    //新品推荐上传数据
    function upload_photo()
    {
        $loader = D($_POST['which_DB']);
        if (!empty($_POST)) {
            $data['title'] = $_POST['title'];
            if($_POST['goodsCode'] != 'null'){
                $data['goodsCode'] = $_POST['goodsCode'];
            }
            $data['time'] = date('Y-m-d H:i:s');
            if (!empty($_FILES['photo'])) {
                $config = array(
                    'rootPath' => './UpdateLoader/new_goods_photo/',
                    'exts' => array('jpg', 'gif', 'png', 'jpeg'),
                );
                $upload = new \Think\Upload($config);// 实例化上传类
                if (($_POST['has_img'] == "has_change" && $_POST['operation'] == "update") || $_POST['operation'] == "add") {
                    $result = $upload->uploadOne($_FILES['photo']);
                }
                if (!$result) {
                    dump($upload->getError());
                } else {
                    $data['image'] = '/ssg/web/UpdateLoader/new_goods_photo/' . $result['savepath'] . $result['savename'];
                }
            }

            if ($_POST['operation'] == "update") {
                $update = $loader->where("id='" . $_POST['hidden_id'] . "'")->save($data);
            } else if ($_POST['operation'] == "add") {
                $update = $loader->add($data);
            }
            if ($update) {
                echo "{\"result\":\"成功\"}";
            } else {
                echo "{\"result\":\"失败\"}";
            }
        } else {
            echo "{\"error\":\"数据添加失败\"}";
        }
    }

    //热销商品
    function BestSellers()
    {
        $this->show_top();
        $page = new \Tools\paged();
        $arr = $page->paged('ssg_host_sale', 6, $_GET['find_goods'],array('title','goodsCode','time'));
        $this->assign('page_list', $arr['page']->fpage());
        $this->assign("goods_list", $arr['result']);
        $this->display();
    }

    //广告图片
    function AdPicture()
    {
        $this->show_top();
        $page = new \Tools\paged();
        $arr = $page->paged("ssg_ad_entrance", 6);
        $this->assign("page_list", $arr['page']->fpage());
        $this->assign("goods_list", $arr['result']);
        $this->display();
    }

    //Banner广告管理(二级菜单)
    function advertisement()
    {
        $page = new \Tools\paged();
        $arr = $page->paged('ssg_banner', 6, $_GET['find_goods'],array('title','goodsCode','time'));
        $this->assign('page_list', $arr['page']->fpage());
        $this->assign("goods_list", $arr['result']);
        $this->assign("num",$arr['num']);
        $this->display();

    }
    //意见反馈（二级菜单）
    function Feedback()
    {
        $page = new \Tools\paged();
        $arr = $page->paged('ssg_feedback', 6, $_GET['find_goods'],array('title','content','system','sendTime'));
        $this->assign('page_list', $arr['page']->fpage());
        $this->assign("goods_list", $arr['result']);
        $this->assign("num",$arr['num']);
        $this->display();

    }
}